import { ILoadMapTypeAll } from '@/common';
import { getBaseUrl } from '@/services/urlconst';
import { getCompanyID, getLevelFirst, getLevelZero, getTenantID, getUserDepID, getUserID, ntkoPageUrlParams } from '@/utils';

/**
 * 合同管理权限
 * @param curRes
 * @returns
 */
const getManageTabSwitch = (curRes: Record<string, any>, pageParams) => {
  //如果是草稿，始终是ownerId
  if (curRes?.['/contractController/ownerID'] && pageParams?.state?.toString() == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1000`) {
    return {
      ownerID: getUserID(),
      params: 'ownerID',
    };
  }

  if (curRes?.['/contractController/all']) {
    return {
      params: 'all',
    };
  } else if (curRes?.['/contractController/ownCompanyID']) {
    return {
      ownCompanyID: getLevelZero(),
      // ownCompanyID: getCompanyID(),
      params: 'ownCompanyID',
    };
  } else if (curRes?.['/contractController/ownDepartmentID']) {
    return {
      ownDepartmentID: getLevelFirst(),
      // ownDepartmentID: getUserDepID(),
      params: 'ownDepartmentID',
    };
  } else if (curRes?.['/contractController/ownerID']) {
    return {
      ownerID: getUserID(),
      params: 'ownerID',
    };
  }
  return {};
};

/** 项目权限信息 */
const updateProjectQuery = (curRes: Record<string, any>) => {
  if (curRes?.['/purchaseSourceController/ownCompanyID']) {
    return {
      ownCompanyID: getLevelZero(),
      params: 'ownCompanyID',
    };
  } else if (curRes?.['/purchaseSourceController/ownDepartmentID']) {
    return {
      ownDepartmentID: getLevelFirst(),
      params: 'ownDepartmentID',
    };
  }
  return {};
};

/** 在线编辑权限路劲处理 */
const ntkoQueryParams = (resObj: Record<string, any>, curRes: Record<string, any>, urlName: string, onlyRead?: boolean) => {
  let wordInfo: Record<string, any> = {
    filedUrl: resObj?.[urlName],
    ntkoWordUrl: '',
  };

  let wordBtnQuery: Record<string, any> = {
    wordAllBtn: false, //全部按钮
    saveBtn: false, //保存按钮
    replaceBtn: false, //替换域按钮
  };

  if (curRes?.['/contractController/readUpdate'] && resObj?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1000` && !onlyRead) {
    wordBtnQuery['saveBtn'] = true;
    wordBtnQuery['wordAllBtn'] = true;
    wordBtnQuery['replaceBtn'] = true;
  } else if (curRes?.['/contractController/readOnly']) {
    wordBtnQuery['wordAllBtn'] = false;
  }

  const wordQuery: string = ntkoPageUrlParams(wordBtnQuery);

  wordInfo['ntkoWordUrl'] = `${getBaseUrl()}/static/NtkoPlugin/index.html?cmd=1&isReadOnly=${wordQuery}&filedUrl=${getBaseUrl()}${wordInfo?.filedUrl}`;

  // wordInfo['ntkoWordUrl'] = `http://127.0.0.1:5500/index.html?cmd=1&isReadOnly=${wordQuery}&filedUrl=${getBaseUrl()}${wordInfo?.filedUrl}`;
  return {
    ...wordInfo,
    ntkoWordUrl: wordInfo?.ntkoWordUrl,
    filedUrl: wordInfo?.filedUrl,
    btnQuery: wordInfo,
  };
};

/**
 * 供应商管理权限
 * @param curRes
 * @returns
 */
const getOppositeQueryInfo = (curRes: Record<string, any>) => {
  if (curRes?.['/supplierInfoController/createCompanyID']) {
    return {
      createCompanyID: getLevelZero(),
      params: 'createCompanyID',
    };
  } else if (curRes?.['/supplierInfoController/createDepartmentID']) {
    return {
      createDepartmentID: getLevelFirst(),
      params: 'createDepartmentID',
    };
  }
  return {};
};

/**
 * 物料管理权限
 * @param curRes
 * @returns
 */
const materialSwitchTable = (curRes: Record<string, any>) => {
  if (curRes?.['/materialController/createCompanyID']) {
    return {
      createCompanyID: getLevelZero(),
      params: 'createCompanyID',
    };
  } else if (curRes?.['/materialController/createDepartmentID']) {
    return {
      createDepartmentID: getLevelFirst(),
      params: 'createDepartmentID',
    };
  } else if (curRes?.['/materialController/createrID']) {
    return {
      createrID: getUserID(),
      params: 'createrID',
    };
  } else {
    return {};
  }
};

/**
 * 合同范本管理
 * @param curRes
 * @returns
 */
const getContractTempTabsSwitch = (curRes: Record<string, any>) => {
  if (curRes?.['/contractTemplateController/all']) {
    return {
      params: 'all',
    };
  } else if (curRes?.['/contractTemplateController/createCompanyID']) {
    return {
      createCompanyID: getLevelZero(),
      params: 'createCompanyID',
    };
  } else if (curRes?.['/contractTemplateController/createDepartmentID']) {
    return {
      createDepartmentID: getLevelFirst(),
      params: 'createDepartmentID',
    };
  }
  return {};
};

export { getManageTabSwitch, updateProjectQuery, ntkoQueryParams, getOppositeQueryInfo, materialSwitchTable, getContractTempTabsSwitch };
